package com.shouer.doc.ui.conversation.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Toast;

import com.shouer.doc.R;
import com.shouer.doc.dialog.DialogDismissListener;

/**
 * Created by apple on 15/10/16.
 */
public class SelectCardDialog extends Dialog implements View.OnClickListener{

    private static Context tmpContext;
    private View mDialogView;
    private View guahao, songyao, cancelView,okView;

    private DialogDismissListener dialogDismissListener;

    public SelectCardDialog(Context context) {
        super(context);
        init(context);
    }

    public SelectCardDialog(Context context, int theme) {
        super(context, theme);
        init(context);
    }

    protected SelectCardDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }

    private void init(Context context) {

        mDialogView = View.inflate(context, R.layout.dialog_conv_select_card_layout, null);
        songyao = mDialogView.findViewById(R.id.songyao);
        guahao = mDialogView.findViewById(R.id.guahao);
        cancelView = mDialogView.findViewById(R.id.cancel);
        okView= mDialogView.findViewById(R.id.ok);

        songyao.setOnClickListener(this);
        guahao.setOnClickListener(this);
        cancelView.setOnClickListener(this);
        okView.setOnClickListener(this);

        setContentView(mDialogView);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.height = ViewGroup.LayoutParams.MATCH_PARENT;
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.gravity= Gravity.CENTER;
        getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
        getWindow().setGravity(Gravity.CENTER);
        getWindow().setWindowAnimations(R.style.share_dialog_animation);

    }

    public SelectCardDialog withCardSelectListener(OnCardSelectListener selectListener) {
        this.cardSelectListener=selectListener;
        return this;
    }

    public SelectCardDialog withDialogDismissListener(DialogDismissListener dialogDismissListener)
    {
        this.dialogDismissListener=dialogDismissListener;
        return this;
    }

    @Override
    public void dismiss()
    {
        super.dismiss();
        if (null!=dialogDismissListener)
        {
            dialogDismissListener.onDialogDismiss();
        }
    }

    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.songyao:
                songyao.setSelected(true);
                guahao.setSelected(false);
                break;
            case R.id.guahao:
                songyao.setSelected(false);
                guahao.setSelected(true);
                break;
            case R.id.cancel:
                dismiss();
                break;
            case R.id.ok:
                int selected=-1;
                if (songyao.isSelected())
                {
                    selected=0;
                }
                if (guahao.isSelected())
                {
                    selected=1;
                }

                if (selected==-1)
                {
                    Toast.makeText(getContext(),"请选择你要扣卡的项目",Toast.LENGTH_SHORT).show();
                }else
                {

                    if (null!=cardSelectListener)
                    {
                        cardSelectListener.onCardSelected(selected);
                    }
                    dismiss();
                }
                break;
        }
    }

    private OnCardSelectListener cardSelectListener;

    public interface OnCardSelectListener
    {
        void onCardSelected(int cardType);
    }
}
